var $email = $("input:eq(0)");
var $code = $("input:eq(1)");
var $name = $("input:eq(3)");
var $password = $("input:eq(4)");
var $password2 = $("input:eq(5)");
var $autoLogin = $("input:eq(6)");
var codeSent = false;

function reg() {
    if (!test_ipt(testEmail, $email)) {
        layer.msg("邮箱地址不正确");
        $email.focus();
        return false;
    }
    if (!codeSent) {
        layer.msg("请先发送邮箱验证消息");
        return false;
    }
    if ($code.val().length < 6 && !isNaN($code.val())) {
        layer.msg("验证码不正确");
        return false;
    }
    if (!test_ipt(testName, $name)) {
        layer.msg("用户名不合法");
        $name.focus();
        return false;
    }
    if (!test_ipt(testPasswd, $password)) {
        layer.msg("密码格式不对");
        $password.focus();
        return false;
    }
    if ($password.val() !== $password2.val()) {
        layer.msg("两次密码输入不一致");
        $password2.focus();
        return false;
    }
    var load = layer.load();
    $.post("/log/reg.do", {
        name: $name.val(),
        password: $password.val(),
        email: $email.val(),
        code: $code.val(),
        autoLogin: $autoLogin[0].checked
    }, function (data) {
        layer.close(load);
        layer.msg(data.message);
        if (data.status > 0) {
            location.assign("/");
        } else switch (data.status) {
            case -1:
        }
    }, "JSON");
}

function getCode() {
    if (!test_ipt(testEmail, $email)) {
        layer.msg("邮箱地址格式不正确");
        $email.focus();
        return false;
    }
    var $this = $("#get-code");
    $this.attr('disabled', 'disabled');
    var load = layer.load();
    $.getJSON("/email/sendEmail.do", {email: $email.val()}, function (data) {
        layer.close(load);
        layer.msg(data.message);
        if (data.status === 0) {
            waitingForNextClick($this, 60, '获取验证码');
            codeSent = true;
            // 邮件发生成功， 再次发起 ajax 监视是否验证成功
            var interval2 = setInterval(function () {
                if ($code.val() === "") {
                    $.getJSON("/email/bindAutoVerify.do", {email: $email.val()}, function (data) {
                        if (data.status === 0) {// 0 验证成功  1 已验证   -1 超时失效   -2 邮箱地址与用户信息不匹配
                            $code.val(data.message);
                            clearInterval(interval2);
                        } else if (data.status === 1) {// 0 验证成功  1 已验证   -1 超时失效   -2 邮箱地址与用户信息不匹配
                            clearInterval(interval2);
                        } else if (data.status === -404) {
                            location.assign('/login.html');
                        } else {
                            if (data.status === -2)
                                layer.msg(data.message);
                        }
                    });
                }
            }, 5000);
        } else if (data.status === -404) {
            location.assign('/login.html');
        }
    });
}
$(function () {
    setEnterKeyEvent($email, getCode);
    setEnterKeyEvent($code, reg);
    setEnterKeyEvent($name, reg);
    setEnterKeyEvent($password, reg);
    setEnterKeyEvent($password2, reg);
});